package com.test.ws.s1.controller;

import com.test.ws.s1.bean.Acl_role;
import com.test.ws.s1.bean.Acl_user;
import com.test.ws.s1.bean.Acl_user_role;
import com.test.ws.s1.service.Acl_userService;
import com.test.ws.s1.service.imp.Acl_roleServiceImp;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

@Controller
public class Acl_roleController {
    @Autowired
    private Acl_roleServiceImp acl_roleServiceImp;

    @Autowired
    private Acl_userService acl_userService;


    /// 查单个
    @RequestMapping("/selectAcl_role")
    public String selectAcl_role(Acl_role acl_role, HttpServletRequest request) {
        String id = request.getParameter("id");

        /// 查询名字
        Acl_user aclUser = acl_userService.selectOneAcl_user(Integer.parseInt(id));
        /// 查询全部的权限
        ArrayList<Acl_role> acl_roles = acl_roleServiceImp.selectAllAcl_role();
        /// 查询对应的权限
        ArrayList<Acl_role> aclRoles = acl_roleServiceImp.selectAcl_role(acl_role, Integer.parseInt(id));
        for (Acl_role aclRole : aclRoles){
             for (Acl_role aclRole1 : acl_roles){
                 if (aclRole.getRole_id() == aclRole1.getRole_id()){
                     aclRole1.setIs_deleted(1);
                 }
             }
        }

        request.setAttribute("acl_roles", acl_roles);
        request.setAttribute("acl", aclUser);
        System.out.println("============================" + aclRoles);
        return "houtai/quanxian";
    }


    /// 查询全部的权限
    @RequestMapping("/selectAllAcl_role")
    public String selectAllAcl_role(HttpServletRequest request) {
        ArrayList<Acl_role> acl_roles = acl_roleServiceImp.selectAllAcl_role();
        request.setAttribute("acl_rolesss", acl_roles);
        return "houtai/quanxianCk";
    }


    /// 删除
    @RequestMapping("/acl_roleDel")
    public String deleteAcl_role(Acl_role acl_role, HttpServletRequest request , Acl_user_role aclUserRole , ArrayList<Acl_user_role> role_id) {
        Acl_user_role aclUserRoles = new Acl_user_role();
        /// 删除
        String id = request.getParameter("roleId");
        System.out.println("============================" + id);
        aclUserRole.setUser_id(Integer.parseInt(id));
        int i = acl_roleServiceImp.deleteAcl_role(aclUserRole);

        /// 增加
        String[] roleIds = request.getParameterValues("role_id");
        for (String roleId : roleIds) {
            aclUserRoles.setUser_id(Integer.parseInt(id));
            aclUserRoles.setRole_id(Integer.parseInt(roleId));
            acl_roleServiceImp.insertAcl_role(aclUserRoles);
        }


        return "houtai/htyu";
    }

}
